import { LOGIN, LOGOUT } from '.././mutation_types'

// localStorage 保存数据时间更久，不会因为关闭标签而丢失

const state = {
  supplier_id: localStorage.getItem('supplier_id') || 0,
  supplier_name: localStorage.getItem('supplier_name') || '未登录',
  stoken: localStorage.getItem('stoken') || ''
}

const getters = {
  supplier_id: state => {
    return state.supplier_id
  },
  supplier_name: state => {
    return state.supplier_name
  },
  stoken: state => {
    return state.stoken
  }
}

const mutations = {
  [LOGIN](state, data) {
    localStorage.setItem('supplier_id', data.id); // 设置localStorage
    localStorage.setItem('supplier_name', data.name);
    localStorage.setItem('stoken', data.stoken);
    state.user_id = data.id;
    state.user_name = data.name;
    state.stoken = data.stoken;
  },
  [LOGOUT](state) {
    localStorage.removeItem('supplier_id'); // 移除localStorage
    localStorage.removeItem('supplier_name');
    localStorage.removeItem('stoken');
    state.supplier_id = 0
    state.supplier_name = '';
    state.stoken = '';
  }
}

export default {
  state,
  mutations,
  getters
}
